package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.enums.NoticeEnum;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.NoticeQueryObject;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") NoticeQueryObject qo){
        PageInfo<Notice> pageInfo = noticeService.query(qo);

        Employee user = UserContext.getCurrentUser();
        model.addAttribute("user",user);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("enums", NoticeEnum.values());
        return "notice/list";
    }


    @RequiredPermission(name="公告删除",expremission="notice:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           noticeService.delete(id);
        }
        return "redirect:/notice/list";
    }


    @RequiredPermission(name="公告新增/编辑",expremission="notice:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Notice notice) {
        if (notice.getId() == null) {
            noticeService.save(notice);
        } else {
            noticeService.update(notice);
        }
        return "redirect:/notice/list";
    }


    @RequiredPermission(name="公告新增/编辑",expremission="notice:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("notice",noticeService.get(id));

        }
        return "notice/input";
    }

    @RequestMapping("/listItem")
    public String listItem(Long id,Model model){
        Notice notice = noticeService.selectById(id);
        model.addAttribute("noticeItem",notice);
        return "notice/input";
    }

    @RequestMapping("/checkId")
    @ResponseBody
    public String checkId(Long id){
        //先通过你当前登录的状态session
        Employee user = UserContext.getCurrentUser();
        //然后判断他这个登录id,和前端按钮传过来的id,进行判断.他是否存在于中间表中
        Long id1 = noticeService.checkId(user.getId(),id);
        //如果存在于中间表中,那么就进行放进数据库里的操作
        if(id1==null){
            noticeService.insertRelation(user.getId(),id);
/*            Notice notice = new Notice();
            notice.setStatus(true);*/
        }
        return "redirect:notice/list";
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public JsonResult updateStatus(Long id){
        boolean b = noticeService.selectByStatus(id);
        noticeService.updateStatus(id,!b);
        return new JsonResult();
    }

}
